package servlet;

import dao.GoodsDao;
import dao.UserDao;
import model.Goods;
import model.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/DisableGoodsServlet.do")
public class DisableGoodsServlet extends HttpServlet {
    private UserService userService;

    public DisableGoodsServlet() {
        this.userService = new UserService(); // 这里需要根据具体的 UserService 实现类进行初始化
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idParam = req.getParameter("id");
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "User ID is required");
            return;
        }


            int Goods_Id = Integer.parseInt(idParam);
            GoodsDao goodsDao = new GoodsDao();
            boolean success2 = goodsDao.delete(Goods_Id);
        if(success2){
            resp.sendRedirect("background1/admin/goodsManager.jsp");
        }else {
            resp.sendRedirect("home/404.html");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String idParam = req.getParameter("id");
        UserDao userDao=new UserDao();
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "User ID is required");
            return;
        }
        try {
            int userId = Integer.parseInt(idParam);
            String username = req.getParameter("username");
            int userType = Integer.parseInt(req.getParameter("user_type"));
            String phone = req.getParameter("phone");

            String email = req.getParameter("email");

            // 封装数据到 User 对象中
            User user = new User();
            user.setId(userId); // 设置 ID
            user.setUsername(username);
            user.setUser_type(userType); // 设置用户类型
            user.setPhone(phone);

            user.setEmail(email);
            for (int i = 0; i <1 ; i++) {
                System.out.println(user.getUser_type());
                System.out.println(user.getId());
                System.out.println(user.getEmail());

                System.out.println(user.getUsername());

            }
            boolean isUpdated = userDao.updateUser(user);

            if (isUpdated) {
                resp.sendRedirect(req.getContextPath() + "/background1/admin/usermanager.jsp");
            } else {
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User update failed");
            }
        } catch (NumberFormatException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid input format");
        }
    }
}
